<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>OC 变量修饰符 | IQ</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/rxios-questions/logo.png">
    <link rel="manifest" href="/rxios-questions/manifest.json">
    <link rel="apple-touch-icon" href="/rxios-questions/icons/apple-touch-icon-152x152.png">
    <link rel="mask-icon" href="/rxios-questions/icons/safari-pinned-tab.svg" color="#3eaf7c">
    <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?2dc091ada8eed930d7a48b1b6523ee7c";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
    </script>
    <meta name="description" content="ios|object-c|swift|面试题|笔试题">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="msapplication-TileColor" content="#000000">
    <meta name="keywords" content="ios,object-c,swift,面试题,笔试题">
    
    <link rel="preload" href="/rxios-questions/assets/css/0.styles.c9cc9f69.css" as="style"><link rel="preload" href="/rxios-questions/assets/js/app.46e01e9b.js" as="script"><link rel="preload" href="/rxios-questions/assets/js/2.a6fd34e2.js" as="script"><link rel="preload" href="/rxios-questions/assets/js/116.5e346d29.js" as="script"><link rel="preload" href="/rxios-questions/assets/js/3.2f1c150a.js" as="script"><link rel="prefetch" href="/rxios-questions/assets/js/10.fe17c06b.js"><link rel="prefetch" href="/rxios-questions/assets/js/100.d9005c35.js"><link rel="prefetch" href="/rxios-questions/assets/js/101.73404cdb.js"><link rel="prefetch" href="/rxios-questions/assets/js/102.ca9ee1a8.js"><link rel="prefetch" href="/rxios-questions/assets/js/103.19dcafdf.js"><link rel="prefetch" href="/rxios-questions/assets/js/104.183ca246.js"><link rel="prefetch" href="/rxios-questions/assets/js/105.6f9f25eb.js"><link rel="prefetch" href="/rxios-questions/assets/js/106.4049ba7b.js"><link rel="prefetch" href="/rxios-questions/assets/js/107.e4a47282.js"><link rel="prefetch" href="/rxios-questions/assets/js/108.897e935c.js"><link rel="prefetch" href="/rxios-questions/assets/js/109.9f0b09a7.js"><link rel="prefetch" href="/rxios-questions/assets/js/11.c62670f2.js"><link rel="prefetch" href="/rxios-questions/assets/js/110.92193699.js"><link rel="prefetch" href="/rxios-questions/assets/js/111.e5d6fb35.js"><link rel="prefetch" href="/rxios-questions/assets/js/112.97eff205.js"><link rel="prefetch" href="/rxios-questions/assets/js/113.be031926.js"><link rel="prefetch" href="/rxios-questions/assets/js/114.358015cc.js"><link rel="prefetch" href="/rxios-questions/assets/js/115.14bce364.js"><link rel="prefetch" href="/rxios-questions/assets/js/117.fab9caa3.js"><link rel="prefetch" href="/rxios-questions/assets/js/118.974b5591.js"><link rel="prefetch" href="/rxios-questions/assets/js/119.53549bdb.js"><link rel="prefetch" href="/rxios-questions/assets/js/12.b0885723.js"><link rel="prefetch" href="/rxios-questions/assets/js/120.b3c13a98.js"><link rel="prefetch" href="/rxios-questions/assets/js/121.d1150318.js"><link rel="prefetch" href="/rxios-questions/assets/js/122.00f528f0.js"><link rel="prefetch" href="/rxios-questions/assets/js/123.b30d7529.js"><link rel="prefetch" href="/rxios-questions/assets/js/124.526d1e24.js"><link rel="prefetch" href="/rxios-questions/assets/js/125.1fc25075.js"><link rel="prefetch" href="/rxios-questions/assets/js/126.d160a4c2.js"><link rel="prefetch" href="/rxios-questions/assets/js/127.21286bf1.js"><link rel="prefetch" href="/rxios-questions/assets/js/128.e7369767.js"><link rel="prefetch" href="/rxios-questions/assets/js/129.99f5d281.js"><link rel="prefetch" href="/rxios-questions/assets/js/13.fb6b4b22.js"><link rel="prefetch" href="/rxios-questions/assets/js/130.b2b5e124.js"><link rel="prefetch" href="/rxios-questions/assets/js/131.521eefd9.js"><link rel="prefetch" href="/rxios-questions/assets/js/132.f73fa098.js"><link rel="prefetch" href="/rxios-questions/assets/js/133.92b503bd.js"><link rel="prefetch" href="/rxios-questions/assets/js/134.080d7f0a.js"><link rel="prefetch" href="/rxios-questions/assets/js/135.fc39e586.js"><link rel="prefetch" href="/rxios-questions/assets/js/136.35cad97b.js"><link rel="prefetch" href="/rxios-questions/assets/js/137.77b8dc35.js"><link rel="prefetch" href="/rxios-questions/assets/js/138.4f9ed89b.js"><link rel="prefetch" href="/rxios-questions/assets/js/139.731ab26f.js"><link rel="prefetch" href="/rxios-questions/assets/js/14.3f79a3be.js"><link rel="prefetch" href="/rxios-questions/assets/js/140.a92b5c09.js"><link rel="prefetch" href="/rxios-questions/assets/js/141.0ca919ef.js"><link rel="prefetch" href="/rxios-questions/assets/js/142.d88d5534.js"><link rel="prefetch" href="/rxios-questions/assets/js/143.8724cb16.js"><link rel="prefetch" href="/rxios-questions/assets/js/144.70d840a6.js"><link rel="prefetch" href="/rxios-questions/assets/js/145.4b34f152.js"><link rel="prefetch" href="/rxios-questions/assets/js/146.d8a96f9d.js"><link rel="prefetch" href="/rxios-questions/assets/js/147.16780c1d.js"><link rel="prefetch" href="/rxios-questions/assets/js/148.377976bd.js"><link rel="prefetch" href="/rxios-questions/assets/js/149.2c94531f.js"><link rel="prefetch" href="/rxios-questions/assets/js/15.8c38a047.js"><link rel="prefetch" href="/rxios-questions/assets/js/150.ee09a875.js"><link rel="prefetch" href="/rxios-questions/assets/js/16.5aae4b13.js"><link rel="prefetch" href="/rxios-questions/assets/js/17.1624c63a.js"><link rel="prefetch" href="/rxios-questions/assets/js/18.9a68cb6d.js"><link rel="prefetch" href="/rxios-questions/assets/js/19.80586715.js"><link rel="prefetch" href="/rxios-questions/assets/js/20.a2648aa8.js"><link rel="prefetch" href="/rxios-questions/assets/js/21.eb8d741e.js"><link rel="prefetch" href="/rxios-questions/assets/js/22.473f9d69.js"><link rel="prefetch" href="/rxios-questions/assets/js/23.d1a411a7.js"><link rel="prefetch" href="/rxios-questions/assets/js/24.faf7d30d.js"><link rel="prefetch" href="/rxios-questions/assets/js/25.e4ade848.js"><link rel="prefetch" href="/rxios-questions/assets/js/26.6ec4ec0f.js"><link rel="prefetch" href="/rxios-questions/assets/js/27.826ed675.js"><link rel="prefetch" href="/rxios-questions/assets/js/28.add63edc.js"><link rel="prefetch" href="/rxios-questions/assets/js/29.a42e1757.js"><link rel="prefetch" href="/rxios-questions/assets/js/30.f73b9234.js"><link rel="prefetch" href="/rxios-questions/assets/js/31.f46d14ce.js"><link rel="prefetch" href="/rxios-questions/assets/js/32.86c399b8.js"><link rel="prefetch" href="/rxios-questions/assets/js/33.afc02978.js"><link rel="prefetch" href="/rxios-questions/assets/js/34.45502382.js"><link rel="prefetch" href="/rxios-questions/assets/js/35.4ecf4a60.js"><link rel="prefetch" href="/rxios-questions/assets/js/36.63898853.js"><link rel="prefetch" href="/rxios-questions/assets/js/37.df649e79.js"><link rel="prefetch" href="/rxios-questions/assets/js/38.b167752f.js"><link rel="prefetch" href="/rxios-questions/assets/js/39.d2788035.js"><link rel="prefetch" href="/rxios-questions/assets/js/4.76211c0f.js"><link rel="prefetch" href="/rxios-questions/assets/js/40.65230100.js"><link rel="prefetch" href="/rxios-questions/assets/js/41.58f676d2.js"><link rel="prefetch" href="/rxios-questions/assets/js/42.9b48c793.js"><link rel="prefetch" href="/rxios-questions/assets/js/43.4644501a.js"><link rel="prefetch" href="/rxios-questions/assets/js/44.5e803f8f.js"><link rel="prefetch" href="/rxios-questions/assets/js/45.9f1e1c87.js"><link rel="prefetch" href="/rxios-questions/assets/js/46.efa0ca31.js"><link rel="prefetch" href="/rxios-questions/assets/js/47.ccab59ea.js"><link rel="prefetch" href="/rxios-questions/assets/js/48.96c05094.js"><link rel="prefetch" href="/rxios-questions/assets/js/49.738c3d62.js"><link rel="prefetch" href="/rxios-questions/assets/js/5.f54fc2e6.js"><link rel="prefetch" href="/rxios-questions/assets/js/50.1ff79b39.js"><link rel="prefetch" href="/rxios-questions/assets/js/51.fb5a9f2b.js"><link rel="prefetch" href="/rxios-questions/assets/js/52.66480d85.js"><link rel="prefetch" href="/rxios-questions/assets/js/53.50d951ac.js"><link rel="prefetch" href="/rxios-questions/assets/js/54.e561519d.js"><link rel="prefetch" href="/rxios-questions/assets/js/55.e79aeb66.js"><link rel="prefetch" href="/rxios-questions/assets/js/56.6ea2d677.js"><link rel="prefetch" href="/rxios-questions/assets/js/57.33268b6a.js"><link rel="prefetch" href="/rxios-questions/assets/js/58.ef489806.js"><link rel="prefetch" href="/rxios-questions/assets/js/59.680c0d59.js"><link rel="prefetch" href="/rxios-questions/assets/js/6.927b46ba.js"><link rel="prefetch" href="/rxios-questions/assets/js/60.6154451b.js"><link rel="prefetch" href="/rxios-questions/assets/js/61.8bc210cd.js"><link rel="prefetch" href="/rxios-questions/assets/js/62.e12f89dc.js"><link rel="prefetch" href="/rxios-questions/assets/js/63.15ae7c4f.js"><link rel="prefetch" href="/rxios-questions/assets/js/64.89c85b6d.js"><link rel="prefetch" href="/rxios-questions/assets/js/65.6d39fb50.js"><link rel="prefetch" href="/rxios-questions/assets/js/66.a70a72a0.js"><link rel="prefetch" href="/rxios-questions/assets/js/67.bd11cce4.js"><link rel="prefetch" href="/rxios-questions/assets/js/68.3e0f6d92.js"><link rel="prefetch" href="/rxios-questions/assets/js/69.5d4bf5c2.js"><link rel="prefetch" href="/rxios-questions/assets/js/7.daa102ed.js"><link rel="prefetch" href="/rxios-questions/assets/js/70.1ae7ef5b.js"><link rel="prefetch" href="/rxios-questions/assets/js/71.28e6bfec.js"><link rel="prefetch" href="/rxios-questions/assets/js/72.0283dabb.js"><link rel="prefetch" href="/rxios-questions/assets/js/73.6678ebad.js"><link rel="prefetch" href="/rxios-questions/assets/js/74.fd4f2720.js"><link rel="prefetch" href="/rxios-questions/assets/js/75.4e4100dc.js"><link rel="prefetch" href="/rxios-questions/assets/js/76.bcd105e8.js"><link rel="prefetch" href="/rxios-questions/assets/js/77.d11ac635.js"><link rel="prefetch" href="/rxios-questions/assets/js/78.b97bb093.js"><link rel="prefetch" href="/rxios-questions/assets/js/79.67f781a6.js"><link rel="prefetch" href="/rxios-questions/assets/js/8.fd3ef487.js"><link rel="prefetch" href="/rxios-questions/assets/js/80.f546dfb0.js"><link rel="prefetch" href="/rxios-questions/assets/js/81.9ff50b58.js"><link rel="prefetch" href="/rxios-questions/assets/js/82.ced57ceb.js"><link rel="prefetch" href="/rxios-questions/assets/js/83.5d833fd5.js"><link rel="prefetch" href="/rxios-questions/assets/js/84.e7248cb4.js"><link rel="prefetch" href="/rxios-questions/assets/js/85.f819db89.js"><link rel="prefetch" href="/rxios-questions/assets/js/86.6cdc0bb9.js"><link rel="prefetch" href="/rxios-questions/assets/js/87.975b2f1b.js"><link rel="prefetch" href="/rxios-questions/assets/js/88.616a8cee.js"><link rel="prefetch" href="/rxios-questions/assets/js/89.305afcb8.js"><link rel="prefetch" href="/rxios-questions/assets/js/9.28e2fcbb.js"><link rel="prefetch" href="/rxios-questions/assets/js/90.854a597e.js"><link rel="prefetch" href="/rxios-questions/assets/js/91.4b02286b.js"><link rel="prefetch" href="/rxios-questions/assets/js/92.b535748f.js"><link rel="prefetch" href="/rxios-questions/assets/js/93.e023325f.js"><link rel="prefetch" href="/rxios-questions/assets/js/94.18c40df8.js"><link rel="prefetch" href="/rxios-questions/assets/js/95.56a9ab23.js"><link rel="prefetch" href="/rxios-questions/assets/js/96.ca00be12.js"><link rel="prefetch" href="/rxios-questions/assets/js/97.f093a89f.js"><link rel="prefetch" href="/rxios-questions/assets/js/98.b2213e85.js"><link rel="prefetch" href="/rxios-questions/assets/js/99.b7635c51.js">
    <link rel="stylesheet" href="/rxios-questions/assets/css/0.styles.c9cc9f69.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/rxios-questions/" class="home-link router-link-active"><img src="/rxios-questions/logo.png" alt="IQ" class="logo"> <span class="site-name can-hide">IQ</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/rxios-questions/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/rxios-questions/oc/" class="nav-link router-link-active">
  Object-C
</a></div><div class="nav-item"><a href="/rxios-questions/swift/" class="nav-link">
  Swift
</a></div><div class="nav-item"><a href="/rxios-questions/article/" class="nav-link">
  文章
</a></div><div class="nav-item"><a href="/rxios-questions/cross-platform/" class="nav-link">
  跨平台
</a></div><div class="nav-item"><a href="/rxios-questions/iq/" class="nav-link">
  面试题
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Language Menu" class="dropdown-title"><span class="title">开发</span> <span class="arrow down"></span></button> <button type="button" aria-label="Language Menu" class="mobile-dropdown-title"><span class="title">开发</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          iOS
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/rxios-questions/oc/" class="nav-link router-link-active">
  Object-C
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/swift/" class="nav-link">
  Swift
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/#" class="nav-link">
  -----
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/library/" class="nav-link">
  Library
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/framework/" class="nav-link">
  Framework
</a></li></ul></li><li class="dropdown-item"><h4>
          native
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/rxios-questions/cross-platform/native/rn.html" class="nav-link">
  react-native
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/cross-platform/native/flutter.html" class="nav-link">
  Flutter
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/cross-platform/native/weex.html" class="nav-link">
  Weex
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/#" class="nav-link">
  -----
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Cordova
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  uni-app
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Taro
</a></li></ul></li><li class="dropdown-item"><h4>
          Web / H5
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Vue
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  React
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Angular
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/#" class="nav-link">
  -----
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Bulma
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Bootstrap
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  AUI
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Amaze UI
</a></li></ul></li></ul></div></div> <a href="https://gitee.com/srxboys/rxios-questions" target="_blank" rel="noopener noreferrer" class="repo-link">
    Gitee
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/rxios-questions/guide/" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="/rxios-questions/oc/" class="nav-link router-link-active">
  Object-C
</a></div><div class="nav-item"><a href="/rxios-questions/swift/" class="nav-link">
  Swift
</a></div><div class="nav-item"><a href="/rxios-questions/article/" class="nav-link">
  文章
</a></div><div class="nav-item"><a href="/rxios-questions/cross-platform/" class="nav-link">
  跨平台
</a></div><div class="nav-item"><a href="/rxios-questions/iq/" class="nav-link">
  面试题
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Language Menu" class="dropdown-title"><span class="title">开发</span> <span class="arrow down"></span></button> <button type="button" aria-label="Language Menu" class="mobile-dropdown-title"><span class="title">开发</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          iOS
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/rxios-questions/oc/" class="nav-link router-link-active">
  Object-C
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/swift/" class="nav-link">
  Swift
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/#" class="nav-link">
  -----
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/library/" class="nav-link">
  Library
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/framework/" class="nav-link">
  Framework
</a></li></ul></li><li class="dropdown-item"><h4>
          native
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/rxios-questions/cross-platform/native/rn.html" class="nav-link">
  react-native
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/cross-platform/native/flutter.html" class="nav-link">
  Flutter
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/cross-platform/native/weex.html" class="nav-link">
  Weex
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/#" class="nav-link">
  -----
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Cordova
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  uni-app
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Taro
</a></li></ul></li><li class="dropdown-item"><h4>
          Web / H5
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Vue
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  React
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Angular
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/#" class="nav-link">
  -----
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Bulma
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Bootstrap
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  AUI
</a></li><li class="dropdown-subitem"><a href="/rxios-questions/" class="nav-link">
  Amaze UI
</a></li></ul></li></ul></div></div> <a href="https://gitee.com/srxboys/rxios-questions" target="_blank" rel="noopener noreferrer" class="repo-link">
    Gitee
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Object-C</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/rxios-questions/oc/" aria-current="page" class="sidebar-link">简介</a></li><li><a href="/rxios-questions/oc/0000-build.html" class="sidebar-link">build - 编译</a></li><li><a href="/rxios-questions/oc/0000-build-script.html" class="sidebar-link">build - script</a></li><li><a href="/rxios-questions/oc/0001-compile-link.html" class="sidebar-link">编译 vs 链接</a></li><li><a href="/rxios-questions/oc/0002-main.html" class="sidebar-link">main - 入口</a></li><li><a href="/rxios-questions/oc/0003-autorelease-pool.html" class="sidebar-link">Autorelease Pool</a></li><li><a href="/rxios-questions/oc/0004-class.html" class="sidebar-link">class - 类</a></li><li><a href="/rxios-questions/oc/0005-class-obj.html" class="sidebar-link">Class vs NSObject</a></li><li><a href="/rxios-questions/oc/0006-new-alloc.html" class="sidebar-link">new vs alloc</a></li><li><a href="/rxios-questions/oc/0007-copy.html" class="sidebar-link">copy - 浅拷贝和深拷贝</a></li><li><a href="/rxios-questions/oc/0008-reference-count.html" class="sidebar-link">reference count - 引用计数</a></li><li><a href="/rxios-questions/oc/0009-instancetype.html" class="sidebar-link">instancetype</a></li><li><a href="/rxios-questions/oc/0010-variable.html" aria-current="page" class="active sidebar-link">OC 变量修饰符</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#一、修饰符" class="sidebar-link">一、修饰符</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#puplic" class="sidebar-link">@puplic</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#private" class="sidebar-link">@private</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#protected-默认情况下所有的实例变量都是protected" class="sidebar-link">@protected (默认情况下所有的实例变量都是protected)</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#package" class="sidebar-link">@package</a></li></ul></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#二、-proprety-参数" class="sidebar-link">二、@proprety 参数</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#atomic" class="sidebar-link">atomic</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#nonatomic" class="sidebar-link">nonatomic</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#retain" class="sidebar-link">retain</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#copy" class="sidebar-link">copy</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#const" class="sidebar-link">const</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#register" class="sidebar-link">register</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#static" class="sidebar-link">static</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#extern" class="sidebar-link">extern</a></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#extern与register的区别" class="sidebar-link">extern与register的区别</a></li></ul></li><li class="sidebar-sub-header"><a href="/rxios-questions/oc/0010-variable.html#三、参考" class="sidebar-link">三、参考</a></li></ul></li><li><a href="/rxios-questions/oc/animate.html" class="sidebar-link">animate - 动画</a></li><li><a href="/rxios-questions/oc/delegate_block.html" class="sidebar-link">Delegate vs Block</a></li><li><a href="/rxios-questions/oc/gesture.html" class="sidebar-link">gesture - 手势</a></li><li><a href="/rxios-questions/oc/import.html" class="sidebar-link">#import</a></li><li><a href="/rxios-questions/oc/kvo-kvc.html" class="sidebar-link">KVO vs KVC</a></li><li><a href="/rxios-questions/oc/lock.html" class="sidebar-link">lock - 锁</a></li><li><a href="/rxios-questions/oc/lock-synchronized.html" class="sidebar-link">@synchronized</a></li><li><a href="/rxios-questions/oc/objc-isa.html" class="sidebar-link">isa</a></li><li><a href="/rxios-questions/oc/page-set-value.html" class="sidebar-link">页面传值</a></li><li><a href="/rxios-questions/oc/struct.html" class="sidebar-link">struce - 结构体</a></li><li><a href="/rxios-questions/oc/thread.html" class="sidebar-link">thread - 线程</a></li><li><a href="/rxios-questions/oc/thread-gcd.html" class="sidebar-link">GCD</a></li><li><a href="/rxios-questions/oc/thread-nsoperation.html" class="sidebar-link">NSOperation</a></li><li><a href="/rxios-questions/oc/timer.html" class="sidebar-link">NSTimer - 定时器</a></li><li><a href="/rxios-questions/oc/xcconfig.html" class="sidebar-link">xcconfig</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>runtime 4-818.2</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>runloop</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Core Foundation</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>框架</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="oc-变量修饰符"><a href="#oc-变量修饰符" class="header-anchor">#</a> OC 变量修饰符</h1> <ul><li>iOS5 之前 所有的 开发都需要开发者自己控制自己的对象的引用和释放。使用的修饰符是 assign、copy、retain</li> <li>iOS5 之后，Apple 推出了ARC(自动引用计数)机制,推出了新的修饰符替代之前的修饰符 strong、weak</li></ul> <h2 id="一、修饰符"><a href="#一、修饰符" class="header-anchor">#</a> 一、修饰符</h2> <h3 id="puplic"><a href="#puplic" class="header-anchor">#</a> @puplic</h3> <ol><li>可以在其他类中访问被@public修饰的成员变量</li> <li>也可以在本类中访问被@public修饰的成员变量</li> <li>可以在子类中访问父类中被@public修饰的成员变量</li></ol> <h3 id="private"><a href="#private" class="header-anchor">#</a> @private</h3> <ol><li>不可可以在其他类中访问被@private修饰的成员变量</li> <li>也可以在本类中访问被@private修饰的成员变量</li> <li>不可以在子类中访问父类中被@private修饰的成员变量</li></ol> <h3 id="protected-默认情况下所有的实例变量都是protected"><a href="#protected-默认情况下所有的实例变量都是protected" class="header-anchor">#</a> @protected (默认情况下所有的实例变量都是protected)</h3> <ol><li>不可可以在其他类中访问被@protected修饰的成员变量</li> <li>也可以在本类中访问被@protected修饰的成员变量</li> <li>可以在子类中访问父类中被@protected修饰的成员变量</li></ol> <h3 id="package"><a href="#package" class="header-anchor">#</a> @package</h3> <p>介于public和private之间的,如果是在其他包中访问就是private,在当前代码中访问就是public.</p> <h2 id="二、-proprety-参数"><a href="#二、-proprety-参数" class="header-anchor">#</a> 二、@proprety 参数</h2> <h3 id="atomic"><a href="#atomic" class="header-anchor">#</a> atomic</h3> <p>设置成员变量的@property属性时，默认为atomic，提供多线程安全。
在多线程环境下，原子操作是必要的，否则有可能引起错误的结果。加了atomic，setter函数会变成下面这样：</p> <div class="language-objc line-numbers-mode"><pre class="language-objc"><code><span class="token punctuation">{</span>lock<span class="token punctuation">}</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>property <span class="token operator">!=</span> newValue<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token punctuation">[</span>property release<span class="token punctuation">]</span><span class="token punctuation">;</span>
  property <span class="token operator">=</span> <span class="token punctuation">[</span>newValue retain<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>unlock<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="nonatomic"><a href="#nonatomic" class="header-anchor">#</a> nonatomic</h3> <p>禁止多线程，变量保护，提高性能。</p> <p>atomic是Objc使用的一种线程保护技术，基本上来讲，是防止在写未完成的时候被另外一个线程读取，造成数据错误。而这种机制是耗费系统资源的，所以在iPhone这种小型设备上，如果没有使用多线程间的通讯编程，那么nonatomic是一个非常好的选择。</p> <h4 id="assign"><a href="#assign" class="header-anchor">#</a> assign</h4> <p>对基础数据类型 （NSInteger，CGFloat）和C数据类型（int, float, double, char）等等。</p> <p>此标记说明设置器直接进行赋值，这也是默认值。在使用垃圾收集的应用程序中，如果你要一个属性使用assign，且这个类符合NSCopying协 议，你就要明确指出这个标记，而不是简单地使用默认值，否则的话，你将得到一个编译警告。这再次向编译器说明你确实需要赋值，即使它是 可拷贝的。</p> <h3 id="retain"><a href="#retain" class="header-anchor">#</a> retain</h3> <p>对其他NSObject和其子类对参数进行release旧值，再retain新值</p> <p>指定retain会在赋值时唤醒传入值的retain消息。此属性只能用于Objective-C对象类型，而不能用于Core Foundation对象。(原因很明显，retain会增加对象的引用计数，而基本数据类型或者Core Foundation对象都没有引用计数——译者注)。
注意: 把对象添加到数组中时，引用计数将增加对象的引用次数+1。</p> <h3 id="copy"><a href="#copy" class="header-anchor">#</a> copy</h3> <p>对NSString 它指出，在赋值时使用传入值的一份拷贝。拷贝工作由copy方法执行，此属性只对那些实行了NSCopying协议的对象类型有效。</p> <p>copy与retain区别：</p> <p>Copy其实是建立了一个相同的对象，而retain不是：</p> <ol><li><p>比如一个NSString 对象，地址为0×1111 ，内容为@”STR”，Copy 到另外一个NSString 之后，地址为0×2222 ，内容相同。</p></li> <li><p>新的对象retain为1 ，旧有对象没有变化retain 到另外一个NSString 之后，地址相同（建立一个指针，指针拷贝），内容当然相同，这个对象的retain值+1。</p></li></ol> <p>总结：retain 是指针拷贝，copy 是内容拷贝。</p> <h4 id="assign与retain"><a href="#assign与retain" class="header-anchor">#</a> assign与retain：</h4> <ol><li>接触过C，那么假设你用malloc分配了一块内存，并且把它的地址赋值给了指针a，后来你希望指针b也共享这块内存，于是你又把a赋值给（assign）了b。此时a和b指向同一块内存，请问当a不再需要这块内存，能否直接释放它？答案是否定的，因为a并不知道b是否还在使用这块内存，如果a释放了，那么b在使用这块内存的时候会引起程序crash掉。</li> <li>了解到1中assign的问题，那么如何解决？最简单的一个方法就是使用引用计数（reference counting），还是上面的那个例子，我们给那块内存设一个引用计数，当内存被分配并且赋值给a时，引用计数是1。当把a赋值给b时引用计数增加到2。这时如果a不再使用这块内存，它只
需要把引用计数减1，表明自己不再拥有这块内存。b不再使用这块内存时也把引用计数减1。当引用计数变为0的时候，代表该内存不再被任何指针所引用，系统可以把它直接释放掉。</li></ol> <h3 id="const"><a href="#const" class="header-anchor">#</a> const</h3> <p>变量修饰符，只有只读权限，意思是这个参数只能读，不能修改内容，谁近修饰谁。</p> <p>用途：</p> <ol><li>便于测试快速找到问题</li> <li>如果是大型算法可以快速找到是哪个模块出现错误</li></ol> <p>修饰位置：</p> <div class="language-objc line-numbers-mode"><div class="highlight-lines"><br><br><div class="highlighted"> </div><br><br><br><div class="highlighted"> </div><br><br><br><div class="highlighted"> </div><br><br><br><div class="highlighted"> </div><br></div><pre class="language-objc"><code>  <span class="token keyword">const</span> <span class="token keyword">int</span> <span class="token operator">*</span>p0 <span class="token operator">=</span> <span class="token operator">&amp;</span>a<span class="token punctuation">;</span>
  p0 <span class="token operator">=</span> <span class="token operator">&amp;</span>c<span class="token punctuation">;</span>
  <span class="token operator">*</span>p0 <span class="token operator">=</span> <span class="token number">50</span><span class="token punctuation">;</span> <span class="token comment">// 1. 指针指向的地址不能修改</span>

  <span class="token keyword">int</span> <span class="token keyword">const</span> <span class="token operator">*</span>p1 <span class="token operator">=</span> <span class="token operator">&amp;</span>a<span class="token punctuation">;</span>
  p1 <span class="token operator">=</span> <span class="token operator">&amp;</span>c<span class="token punctuation">;</span>
  <span class="token operator">*</span>p1 <span class="token operator">=</span> <span class="token number">50</span><span class="token punctuation">;</span> <span class="token comment">// 1. 指针指向的地址不能修改</span>

  <span class="token keyword">int</span> <span class="token operator">*</span> <span class="token keyword">const</span> p2 <span class="token operator">=</span> <span class="token operator">&amp;</span>a<span class="token punctuation">;</span>
  p2 <span class="token operator">=</span> <span class="token operator">&amp;</span>c<span class="token punctuation">;</span>
  <span class="token operator">*</span>p2 <span class="token operator">=</span> <span class="token number">50</span><span class="token punctuation">;</span> <span class="token comment">// 2. 指针本身不能修改</span>

  <span class="token keyword">const</span> <span class="token keyword">int</span> <span class="token operator">*</span> <span class="token keyword">const</span> p3 <span class="token operator">=</span> <span class="token operator">&amp;</span>a<span class="token punctuation">;</span>
  p3 <span class="token operator">=</span> <span class="token operator">&amp;</span>c<span class="token punctuation">;</span>
  <span class="token operator">*</span>p3 <span class="token operator">=</span> <span class="token number">50</span><span class="token punctuation">;</span> <span class="token comment">// 3. 指针本身不能修改，并且他指向的地方也不能修改</span>
</code></pre><div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="register"><a href="#register" class="header-anchor">#</a> register</h3> <div class="language-objc line-numbers-mode"><pre class="language-objc"><code> <span class="token comment">//register int b = 0;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>变量前面加register修饰，编译器会优先将此变量放在寄存器中，这样对这个变量进行的各种操作及运算，会很快，（适用于嵌入式编程）当然CPU周边的用户可用空闲寄存器是有限的，所以当定义多个register修饰的变量后，当可用寄存器已被占完，后面的变量即便用register修饰，也是不起作用的，依然是在普通内存中为变量开辟空间。</p> <h3 id="static"><a href="#static" class="header-anchor">#</a> static</h3> <p>有时候我们非常希望，用一个小变量记录某函数调用的次数，解决这个问题</p> <ol><li>可以使用全局变量；但是由于所有函数都可以修改它，对于较大程序出了问题不好调，所以使用全局变量并不好。</li> <li>可以在目标函数中定义一个static变量（测试会发现这个变量的地址已经不再栈区，而是在数据区），每次进入函数让其++，static作用在局部变量前，函数结束此变量的值不清空，即改变了此变量的生命期，而且直到整个程序结束，并且此变量的值只有在定义它的函数中才可以被使用和重新赋值</li></ol> <h3 id="extern"><a href="#extern" class="header-anchor">#</a> extern</h3> <div class="language-objc line-numbers-mode"><pre class="language-objc"><code><span class="token keyword">extern</span> <span class="token keyword">int</span> n<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>extern可以扩展全局变量或函数的作用范围，只有全局变量或函数才可以用extern扩展</p> <h3 id="extern与register的区别"><a href="#extern与register的区别" class="header-anchor">#</a> extern与register的区别</h3> <p>extern可以用来扩展函数的作用范围，可以跨文件扩展，前提是被扩展全局变量或者函数在定义时没有被static修饰！</p> <p>简单来说，assign 修饰符用来修饰值类型和 id 类型（一般是 delegate）的属性，需要注意的是如果 id 类型的属性的修饰符用了 assign, 当你不再需要这个属性时，你必须将 delegate 手动置空，防止野指针产生，这也是为什么 delegate 一般都用 weak 修饰的原因。</p> <p>weak 和 strong 是 ARC 的产物，分别表示弱引用类型和强引用类型。在给 strong 修饰的属性赋值时，会将属性的指针指向新值的地址，同时持有这个新值（新值的引用计数被加1）。在给 weak 修饰的属性赋值时，只是简单地将属性的指针指向新值的地址，不会持有新值。</p> <p>retain 在 ARC 下基本等同于 strong。</p> <p>copy 表示强引用，但是不会持有新值，而是拷贝一份引用计数为1的值给属性。给这种修饰符修饰的属性赋值时，实际上是把 新值调用 copy 方法后的返回值 赋给属性。NSString 类型常常用 copy 修饰，就是防止修改某个对象后，对强引用这个对象的属性造成不必要影响。</p> <p>引用类型默认的修饰符是 strong.</p> <p>值类型的默认修饰符是 assign.</p> <h2 id="三、参考"><a href="#三、参考" class="header-anchor">#</a> 三、参考</h2> <ul><li><a href="https://www.jb51.net/article/125546.htm" target="_blank" rel="noopener noreferrer">jb51 - iOS 各种修饰符的区别汇总(推荐)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.jianshu.com/p/c4b928161a69" target="_blank" rel="noopener noreferrer">iOS中常量、变量以及属性的修饰符关键字使用<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://zhuanlan.zhihu.com/p/363790304" target="_blank" rel="noopener noreferrer">最新整理：iOS面试题-面试常问问题（一）<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div> <footer class="page-edit"><div class="edit-link"><a href="https://gitee.com/srxboys/rxios-questions/edit/master/docsmd/oc/0010-variable.md" target="_blank" rel="noopener noreferrer">帮助改善此页面！</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">4/12/2021, 6:34:41 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/rxios-questions/oc/0009-instancetype.html" class="prev">
        instancetype
      </a></span> <span class="next"><a href="/rxios-questions/oc/animate.html">
        animate - 动画
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/rxios-questions/assets/js/app.46e01e9b.js" defer></script><script src="/rxios-questions/assets/js/2.a6fd34e2.js" defer></script><script src="/rxios-questions/assets/js/116.5e346d29.js" defer></script><script src="/rxios-questions/assets/js/3.2f1c150a.js" defer></script>
  </body>
</html>
